<template>
  <div class="registerAccount_container padding-1">
		<div class="emailLogin_container_card br-16 padding-1">
			<p class="mb-0">{{$t("邮箱地址")}}</p>
			<input class="mb-0" type="text" v-model="email">
			<p class="mb-0">{{$t("密码")}}</p>
			<input class="mb-0" type="password" v-model="newPassword">
			<p class="mb-0">{{$t("确认密码")}}</p>
			<input class="mb-0" type="password" v-model="againPassword">
			<p class="mb-0">{{$t("昵称")}}</p>
			<input class="mb-0" type="text" v-model="nickname">
			<p class="mb-0">{{$t("邀请码")}}</p>
			<input class="mb-0" type="text" v-model="invitationCode">
			<p class="mb-0">{{$t("验证码")}}</p>
			<div class="card_code mb-0">
				<input class="mb-0" type="text" v-model="code">
				<span @click="sendCode">{{$t("发送")}}</span>
			</div>
			
			<div class="loginBtns_item mt-1" @click="loginByMetamask" v-if="showWalletBind">
				<div class="loginBtns_item_img">
					<img src="@/image/login/mt.png" alt="">
				</div>
				<div class="loginBtns_item_title">
					{{ $t("绑定钱包") }}
				</div>
			</div>

			<button class="mt-2" @click="doresgister">{{$t("注册")}}</button>
		</div>
	</div>
</template>

<script>
import {refisterByEmail , sendCodeToEmail , registerNew} from "@/request/login";
import { checkWallet, connectWallet } from "@/api/index";
export default {
	name: "RegisterAccount",
	data(){
		return{
			nickname:'',
			email:'',
			newPassword:'',
			againPassword:'',
			invitationCode: '',
			code:'',
			walletAddress: '',
			sign:'',
		}
	},
	computed:{
		showWalletBind(){
			return checkWallet()
		}
	},
	mounted(){
		// 获取邀请链接
		this.setIncomingInviteCode();
	},
	methods:{
		setIncomingInviteCode(){
			let inviteCode = localStorage.getItem('invite_code');
			if(inviteCode){
				this.invitationCode = inviteCode
			}
		},

		doresgister(){
			if( this.email ===''|| this.newPassword ===''|| this.againPassword ===''){
				this.$toast.fail(this.$t("请输入邮箱和密码"))
				return 
			}else if(this.invitationCode === ''){
				this.$toast.fail(this.$t("请输入邀请码"))
			}else if( this.newPassword !== this.againPassword )	{
				this.$toast.fail(this.$t("两次密码输入不一致"))
				return
			}else if(checkWallet() && this.walletAddress === ''){
				this.$toast.fail(this.$t("请绑定钱包"))
				return
			}
			// 注册接口引入
			let obj = {
				username:this.email,
				email:this.email,
				password:this.newPassword,
				repassword:this.againPassword,
				code:this.invitationCode,
				captcha:this.code,
				wallet_address: this.walletAddress,
				sign: this.sign,
				login_type: checkWallet() ? 2 : 1,
				nickname: this.nickname,
			}
			// 需更改
			registerNew(obj).then(res=>{
				if(res.code === 1){
					this.$toast.success(this.$t("注册成功"))
					this.$router.push({name:'Login'})
				}else{
					this.$toast.fail(res.msg)
				}
			})
		},
		async loginByMetamask(){
			// 钱包登录
			console.log("钱包登录")
			const res = await connectWallet()
			if(!!res.useAccount){
				// 返回钱包地址
				console.log(res)
				this.$toast.success(this.$t("获取成功"))
				this.walletAddress = res.useAccount
				this.sign = res.sign
			}else{
				this.$toast.fail(this.$t("钱包验证失败"))
				// this.walletAddress = ''
				// localStorage.removeItem('useAccount')
				// this.sign = ''
			}
		},
		sendCode(){
			// 发送验证码
			if(this.email ===''){
				this.$toast.fail(this.$t("请输入邮箱"))
			}else{
				let obj = {
					email:this.email,
					event:'register'
				}
				sendCodeToEmail(obj).then(res=>{
					if(res.code === 1){
						this.$toast.success(this.$t("验证码发送成功"))
					}else{
						this.$toast.fail(res.msg)
					}
				})
			}
		},
	}
}
</script>

<style>

</style>